//
//  NewDetailDataBase.m
//  NewProject
//
//  Created by 李赐岩 姜雪 张昕锐 on 15/3/10.
//  Copyright (c) 2015年 李赐岩 姜雪 张昕锐. All rights reserved.
//

#import "NewDetailDataBase.h"

@implementation NewDetailDataBase

+(instancetype)shareDetailDataBaseHandle
{
    static NewDetailDataBase *newDeatilDBH = nil;
    if (newDeatilDBH == nil) {
        newDeatilDBH = [[NewDetailDataBase alloc] init];
    }
    return newDeatilDBH;
}

static sqlite3 *detailDB;

-(void)openDB
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentFielPath = [array lastObject];
    
    NSString *sqlitePath = [documentFielPath stringByAppendingPathComponent:@"detail.sqlite"];
    if (detailDB != nil) {
        return;
    }
    
    int result = sqlite3_open(sqlitePath.UTF8String, &detailDB);
    
    if (result == SQLITE_OK) {
        NSLog(@"数据库打开成功");
    }else
    {
        NSLog(@"数据库打开失败");
    }
    
}

-(void)creatTable
{
    NSString *creatSql = @"CREATE TABLE IF NOT EXISTS detailClass(number INTEGER PRIMARY KEY AUTOINCREMENT, imagestr TEXT, title TEXT, urlStr TEXT)";
    
    int result = sqlite3_exec(detailDB, creatSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
    }
    
    
}

-(void)insertDeatail:(NewDetailModel *)detail
{
    NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO detailClass(imagestr, title, urlStr) VALUES('%@', '%@', '%@')", detail.imagestr, detail.title, detail.urlStr];
    
    int result = sqlite3_exec(detailDB, insertSql.UTF8String, NULL, NULL, nil);
    
    NSLog(@"%d", result);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else
    {
        NSLog(@"插入失败");
    }
    
}

-(void)deleteDetail:(NSInteger)number
{
    NSString *deleteSql = [NSString stringWithFormat:@"DELETE FROM detailClass WHERE number = '%ld'", (long)number];
    
    int result = sqlite3_exec(detailDB, deleteSql.UTF8String, NULL, NULL, nil);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else
    {
        NSLog(@"删除失败");
    }
    
    
}

-(NSMutableArray *)selectDetail
{
    NSString *selectSql = @"SELECT * FROM detailClass";
    
    sqlite3_stmt *stmt = nil;
    
    int result = sqlite3_prepare_v2(detailDB, selectSql.UTF8String, -1, &stmt, nil);
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        int number = sqlite3_column_int(stmt, 0);
        const unsigned char *image = sqlite3_column_text(stmt, 1);
        const unsigned char *title = sqlite3_column_text(stmt, 2);
        const unsigned char *url = sqlite3_column_text(stmt, 3);
        
        NSInteger numberInteger = number;
        NSString *imageStr = [NSString stringWithUTF8String:(const char *)image];
         NSString *titleStr = [NSString stringWithUTF8String:(const char *)title];
         NSString *urlStr = [NSString stringWithUTF8String:(const char *)url];
        
        NewDetailModel *detail = [[NewDetailModel alloc] init];
        detail.imagestr = imageStr;
        detail.title = titleStr;
        detail.urlStr = urlStr;
        detail.number = numberInteger;
        [array addObject:detail];
        
    }
    }else{
        NSLog(@"查询准备失败");
    }
    return array;
}




@end
